package cn.hdu.liang.core.controller.admin;

import cn.hdu.liang.core.bean.product.Brand;
import cn.hdu.liang.core.service.product.BrandService;
import cn.itcast.common.page.Pagination;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class BrandController {

    @Autowired
    private BrandService brandService;

    //品牌列表页面
    @RequestMapping(value = "/brand/list.do")
    public String list(String name, Integer isDisplay, Integer pageNo, ModelMap modelMap) {

        StringBuilder params = new StringBuilder();
        Brand brand = new Brand();
        if(StringUtils.isNotBlank(name)) {
            brand.setName(name);
            params.append("name=").append(name);
        }

        if(isDisplay != null) {
            brand.setIsDisplay(isDisplay);
            params.append("&").append("isDisplay=").append(isDisplay);
        } else {
            brand.setIsDisplay(1);
            params.append("&").append("isDisplay=").append(1);
        }

        //页号，如果页号为空或者小于1，则置为1
        brand.setPageNo(Pagination.cpn(pageNo));

        //每页数
        brand.setPageSize(5);
        Pagination pagination = brandService.getBrandListWithPage(brand);

        //分页展示
        String url = "/brand/list.do";
        pagination.pageView(url, params.toString());

        modelMap.addAttribute("pagination", pagination);
        modelMap.addAttribute("name", name);
        modelMap.addAttribute("isDisplay", isDisplay);
        return "brand/list";
    }

    //跳转品牌添加页面
    @RequestMapping(value = "/brand/toAdd.do")
    public String toAdd() {
        return "brand/add";
    }

    //添加品牌
    @RequestMapping(value = "/brand/add.do")
    public String add(Brand brand) {
        brandService.addBrand(brand);
        return "redirect:/brand/list.do";
    }

    //删除一个品牌
    @RequestMapping(value = "/brand/delete.do")
    public String delete(Integer id, String name, Integer isDisplay, ModelMap modelMap) {

        brandService.deleteBrandByKey(id);

        if(StringUtils.isNotBlank(name)) {
            modelMap.addAttribute("name", name);
        }

        if(isDisplay != null) {
            modelMap.addAttribute("isDisplay", isDisplay);
        }

        return "redirect:/brand/list.do";
    }

    //删除多个品牌
    @RequestMapping(value = "/brand/deletes.do")
    public String deletes(Integer[] ids, String name, Integer isDisplay, ModelMap modelMap) {

        brandService.deleteBrandByKeys(ids);

        if(StringUtils.isNotBlank(name)) {
            modelMap.addAttribute("name", name);
        }

        if(isDisplay != null) {
            modelMap.addAttribute("isDisplay", isDisplay);
        }

        return "redirect:/brand/list.do";
    }

    //跳转到修改页面
    @RequestMapping(value = "/brand/toEdit.do")
    public String toEdit(Integer id, ModelMap modelMap) {

        Brand brand = brandService.getBrandByKey(id);
        modelMap.addAttribute("brand", brand);
        return "brand/edit";
    }

    //修改页面
    @RequestMapping(value = "/brand/edit.do")
    public String edit(Brand brand, ModelMap modelMap) {
        brandService.updateBrandByKey(brand);
        return "redirect:/brand/list.do";
    }
}
